package org.supsi.skarabee.main;

import org.supsi.skarabee.bt.BTConnnectionManager;
import org.suspi.skarabee.monitoring.MonitorActivity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.merge.skarabee.R;

public class InputMethodActivity extends Activity implements OnClickListener{

	private final int REQUEST_ENABLE_BT = 1;
	private Button buttonConnect;
	private Button buttonRead;
	private BTConnnectionManager btConnnectionManager;
	private BluetoothAdapter bluetoothAdapter;


	public void readMacFromUser(View view){
		EditText editText = (EditText)findViewById(R.id.editTextMA);
		String mac = editText.getText().toString();
		if (isValid(mac)){
			//Toast.makeText(getApplicationContext(), "the mac addess is well-formatted " + editText.getText().toString() , Toast.LENGTH_SHORT).show();
		}else{
			//Toast.makeText(getApplicationContext(), "the mac addess is not formatted correctly " + editText.getText().toString() , Toast.LENGTH_SHORT).show();
		}
	}


	private boolean isValid(String mac){
		return mac.matches("([\\da-fA-F]{2}(?:\\:|-|$)){6}");
	}


	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
		if (scanResult != null && scanResult.getContents() != null) {
			String mac = scanResult.getContents().split(" ")[0];
			String port = scanResult.getContents().split(" ")[1];
			if(isValid(mac)){
				//Toast.makeText(getApplicationContext(), "the mac addess is well-formatted " + mac + " " + port, Toast.LENGTH_SHORT).show();
				//this.macAddress = mac;
			}else{
				//Toast.makeText(getApplicationContext(), "the mac addess is not formatted correctly " + mac + " " + port, Toast.LENGTH_SHORT).show();
			}
			
			startConnection(mac, port);
		}
		// else continue with any other code you need in the method
		;
	}
	


	private void startQRScan(){
		IntentIntegrator integrator = new IntentIntegrator(this);
		integrator.initiateScan();
	}

	private void startConnection(String mac, String port){
		this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (bluetoothAdapter == null) {
			
			/*TODO popup
			 * 
			 */
			// Device does not support Bluetooth
			
		}

		if (!bluetoothAdapter.isEnabled()) {
			Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
		
		btConnnectionManager = BTConnnectionManager.getInstance(mac, Integer.parseInt(port), bluetoothAdapter);
		
		btConnnectionManager.startConnection();
		
		Intent monitorIntent = new Intent(getApplicationContext(), MonitorActivity.class);
		startActivity(monitorIntent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_input_method);
		
		buttonConnect = (Button) findViewById(R.id.nodeMainContainer);
		buttonRead = (Button) findViewById(R.id.buttonScanQR);	
		buttonConnect.setOnClickListener(this);
		buttonRead.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.nodeMainContainer:
				;
			break;
		case R.id.buttonScanQR:
			startQRScan();
				;
			break;
		default:
			break;
		}
	}

}
